home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
8174
/
8174.xpi
/
chrome
/
antbar.jar
/
content
/
rank
/
rank.js
Wrap
Text File
|
2009-12-30
|
8KB
|
280 lines
/*
* DO NOT EDIT THIS FILE !
*
* IT WAS AUTOMATICALLY COPIED FROM THE /lib/ DIRECTORY
* TO UPDATE IT YOU NEED TO BUILD THE XPI OF THE CURRENT PROJECT
*
*/
//
// rank.js
// firefox
//
// Created by Seed on 2008-10-23.
// Copyright 2008-2009 Ant.com. All rights reserved.
//
var AntRankVersion = 1;
AntLib.toLog("AntRankService including, typeof(AntRankService):" + typeof(AntRankService));
var AntRankService = (typeof(AntRankService) == 'undefined' || AntRankService.version < AntRankVersion) ?
{
url: null,
UUID: null,
rankButton: null,
version: AntRankVersion,
inited: null,
init: function()
{
var self = AntRankService;
if (self.inited)
{
AntLib.toLog("AntRankService version " + self.version + ' already initialized');
return;
}
AntLib.toLog("AntRankService initializing version " + self.version);
try {
self.initUid();
self.rankButtons = new AntArray();
self.rankContainers = new AntArray();
AntWatchUrl.addWatcher(
function (aURI)
{
self.updateRank(aURI);
});
} catch (e) {
AntLib.toLog('AntRankService initializing failed: ' + e);
}
self.inited = true;
},
getRankBranch: function()
{
return AntLib.CCSV("@mozilla.org/preferences-service;1", "nsIPrefService")
.getBranch("extensions.antrankservice.");
},
getAntBarUuid: function()
{
var self = AntRankService;
try {
var pref = AntLib.CCSV("@mozilla.org/preferences-service;1", "nsIPrefService")
.getBranch("extensions.anttoolbar.");
var uuid = pref.getCharPref("uuid");
if (uuid == null || uuid.length == 0)
return null;
return '{' + uuid.toUpperCase().replace(/[{}]/g, '') + '}';
} catch (e) {
return null;
}
},
initUid: function()
{
var self = AntRankService;
var pref = self.getRankBranch();
var uuid = pref.getCharPref("uuid");
if (!uuid)
{
uuid = self.getAntBarUuid();
if (!uuid)
{
uuid = Components.classes["@mozilla.org/uuid-generator;1"].
getService(Components.interfaces.nsIUUIDGenerator).
generateUUID().toString().toUpperCase();
}
pref.setCharPref("uuid", uuid);
}
self.UUID = uuid;
},
isRankEnable: function()
{
var self = AntRankService;
if (typeof(self.rankButtons) == 'undefined')
{
AntLib.toLog('isRankEnable error: self.rankButtons undefined');
return false;
}
AntLib.toLog('isRankEnable, rankButtons number: ' + self.rankButtons.length);
return self.rankButtons.length != 0;
},
removeButtonId: function (rankButtonId, rankVisibleId)
{
var self = AntRankService;
var button = AntLib.ob(rankButtonId);
AntLib.toLog('hidding ' + rankButtonId);
self.rankButtons.removeValue(button);
self.rankContainers.removeValue(button);
button.setAttribute('hidden', true);
if (typeof(rankVisibleId) != 'undefined')
{
AntLib.toLog('hidding ' + rankVisibleId);
button = AntLib.ob(rankVisibleId);
self.rankContainers.removeValue(button);
button.setAttribute('hidden', true);
}
},
addButtonId: function (rankButtonId, rankVisibleId)
{
var self = AntRankService;
var button = AntLib.ob(rankButtonId);
AntLib.toLog('addButtonId Info: adding: ' + rankButtonId);
if (button == null)
{
AntLib.toLog('addButtonId Warning: unknown ID: ' + rankButtonId);
}
if (button != null && !self.rankButtons.inArray(button))
self.rankButtons.push(button);
if (typeof(rankVisibleId) != 'undefined')
{
var friendOfVisibility = AntLib.ob(rankVisibleId);
if (!self.rankContainers.inArray(friendOfVisibility))
self.rankContainers.push(friendOfVisibility);
}
if (!self.rankContainers.inArray(button))
self.rankContainers.push(button);
},
updateVisible: function()
{
var self = AntRankService;
for (var j = 0; j < self.rankContainers.length; j++)
{
AntLib.toLog('setting hidden false to: ' + self.rankContainers[j].id);
self.rankContainers[j].setAttribute('hidden', false);
}
},
isValidRankUrl: function (url)
{
var ios = AntLib.CCSV('@mozilla.org/network/io-service;1', "nsIIOService");
try
{
var uri = ios.newURI(url, null, null);
return (url.match(/^https?:\/\//i) && uri.host != "localhost");
}
catch (e)
{
AntLib.toLog("error isValidRankUrl : " + e);
return false;
}
},
cleanRank: function ()
{
var self = AntRankService;
self.url = null;
var lab = AntLang.getString("AntRank.noRank");
},
setLabelRank: function (rank)
{
var self = AntRankService;
if (!rank || !parseInt(rank, 10))
rank = AntLang.getString("AntRank.noRank");
else
rank = AntLang.getString("AntRank.Rank") + ": " + rank;
rank = AntLib.trim(rank);
var size = 9 * rank.length;
var minwidth = size < 40 ? 40 : size
for (var i = 0; i < self.rankButtons.length; i++)
{
self.rankButtons[i].setAttribute("label", rank);
self.rankButtons[i].setAttribute("minwidth", minwidth);
}
},
fetchRank: function (url)
{
var self = AntRankService;
try {
var body = "url=" + AntLib.uriEncode(url) +
"&ref=" + AntLib.uriEncode(gBrowser.contentDocument.referrer) +
"&uid=" + self.UUID;
}
catch(e) {
AntLib.toLog("Error forming RANK body : " + e);
return;
}
var httpRequest = new XMLHttpRequest();
httpRequest.open("POST", "http://rank.ant.com/fetch/", true);
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
httpRequest.onload = function ()
{
var rank = httpRequest.responseText;
gBrowser.contentDocument.__antrank__ = rank;
self.setLabelRank(rank);
};
httpRequest.send(body);
},
updateRank: function (aURI)
{
var self = AntRankService;
try {
if ( self.isRankEnable() == false )
return;
self.cleanRank();
self.url = aURI;
var rank = null;
if (self.isValidRankUrl(self.url.spec))
{
if (gBrowser.contentDocument.__antrank__)
rank = gBrowser.contentDocument.__antrank__;
else
{
self.fetchRank(self.url.spec);
return;
}
}
self.setLabelRank(rank);
} catch (e) {
AntLib.toLog('updateRank error: ' + e);
}
},
goProfil: function ()
{
var self = AntRankService;
if (!self.isValidRankUrl(self.url.spec))
return;
var domain = self.url.host;
if (domain)
{
AntLib.openURL('http://www.ant.com/site/' + domain, true);
}
}
} : AntRankService;